package cn.didi.feidi.androidorder.fragment.personal;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.activity.personal.PersonReturnDepositActivity;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;


public class PersonBoxMoneyFragment extends FeidiBaseFragment {

    private static final String TAG = "PersonBoxMoneyFragment";
    int blankBucket;
    private TextView blankBucketNumber;
    private TextView allBoxMoneyValue;
    private TextView boxMoneyToWalletBalance;
    private Double deposit;//押金

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonBoxMoneyFragment newInstance(Bundle bundle) {
        PersonBoxMoneyFragment fragment = new PersonBoxMoneyFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_box_money, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }

    @Override
    protected void initViews(View rootView) {
        blankBucketNumber = (TextView) rootView.findViewById(R.id.person_box_money_blank_bucket_number);
        allBoxMoneyValue = (TextView) rootView.findViewById(R.id.person_box_money_values);
        boxMoneyToWalletBalance = (TextView) rootView.findViewById(R.id.person_box_money_to_wallet_balance);
    }

    @Override
    protected void initData() {
        // 初始化 blankBucketNumber 和 allBoxMoneyValue
        allBoxMoneyValue.setText(String.valueOf(UserManager.getDeposit()));
        blankBucketNumber.setText(String.valueOf((int) Math.rint(UserManager.getDeposit() / 50)));
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();

        params.put("userId", UserManager.getUserId());
        params.put("token", UserManager.getToken());

        executeRequest(new AppActionRequest<Double>(Api.GET_DEPOSIT, params,
                new TypeToken<ApiResponse<Double>>() {
                }.getType(), new ActionCallbackListener<Double>() {
            @Override
            public void onSuccess(String message, Double data) {
                if (data != null) {
                    deposit = data;
                    blankBucket = (int) Math.rint(data / 50);

                    UserManager.setDeposit(data);
                    allBoxMoneyValue.setText(String.valueOf(data));
                    blankBucketNumber.setText(String.valueOf(blankBucket));
                } else {
                    makeToast("success" + message);
                    Log.d(TAG, message);
                }
            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);

                switch (errorEvent) {
                    default:
                        makeToast("未知错误");
                        break;
                }
            }
        }).setEmergency(true));
    }

    @Override
    protected void addListener() {
        boxMoneyToWalletBalance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (deposit == null || deposit.equals(new Double(0))) {
                    makeToast("空桶个数为 0 ");
                    return;
                }

                Intent intent = new Intent(getContext(), PersonReturnDepositActivity.class);
                intent.putExtra("blankBucket", blankBucket);
                startActivity(intent);
            }
        });
    }

}
